package setting

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"mall_admin_api/biz/dal"
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin"
	"mall_admin_api/biz/model/admin_menu"
	"mall_admin_api/biz/pack"
)

func (s *Setting) CreateMenu(ctx context.Context, req *admin_menu.CreateMenuRequest) error {
	data := &model.AdminMenu{
		IFrame:        req.Iframe,
		Name:          req.Name,
		Component:     req.Component,
		Pid:           req.PID,
		Sort:          req.Sort,
		Icon:          req.Icon,
		Path:          req.Path,
		Cache:         req.Cache,
		Hidden:        req.Hidden,
		ComponentName: req.ComponentName,
		Permission:    req.Permision,
		Type:          req.Type,
		Router:        req.Router,
		RouterMethod:  req.RouterMethod,
		CreatedBy:     s.commonInfo.GetAdministratorInfo().Nickname,
	}

	if err := s.dao.Q.Transaction(func(tx *dal.Query) error {
		adminMenu, err := s.dao.AdminMenuDAO.CreateAdminMenuWithTx(ctx, tx, data)
		if err != nil {
			hlog.CtxErrorf(ctx, "dao.AdminMenuDAO.CreateAdminMenuWithTx error: %v", err)
			return err
		}
		var adminMenuRelation = &model.AdminRoleMenuRelation{
			AdminMenuID: adminMenu.ID,
			AdminRoleID: 1,
		}
		_, err = s.dao.AdminRoleMenuRelationDAO.CreateAdminRoleMenuRelationWithTx(ctx, tx, adminMenuRelation)
		if err != nil {
			hlog.CtxErrorf(ctx, "s.dao.AdminRoleMenuRelationDAO.CreateAdminRoleMenuRelationWithTx error: %v", err)
			return err
		}
		return nil
	}); err != nil {
		hlog.CtxErrorf(ctx, "transaction err: %v", err)
		return err
	}

	return nil
}

func (s *Setting) UpdateMenu(ctx context.Context, req *admin.UpdateMenuRequest) (*admin.Menu, error) {
	//var fields []field.AssignExpr
	//fields = append(fields, dal.AdminMenu.Name.Value(req.Name), dal.AdminMenu.ParentID.Value(req.PID))
	//if err := dal.UpdateAdminMenu(ctx, req.ID, fields); err != nil {
	//	hlog.CtxErrorf(ctx, "UpdateMenu error: %v", err)
	//	return nil, err
	//}
	return admin.NewMenu(), nil
}

func (s *Setting) DeleteMenu(ctx context.Context, req *admin_menu.DeleteMenuRequest) error {
	if err := s.dao.AdminMenuDAO.DeleteAdminMenu(ctx, []gen.Condition{dal.AdminMenu.ID.In(req.IDs...)}); err != nil {
		hlog.CtxErrorf(ctx, "DeleteMenu error: %v", err)
		return err
	}
	return nil
}

func (s *Setting) MenuBuild(ctx context.Context) ([]*admin_menu.AdminMenuBuild, error) {
	list, err := s.dao.AdminRoleMenuRelationDAO.GetMenuListByAdminID(ctx, s.commonInfo.GetAdministratorInfo().ID)
	if err != nil {
		hlog.CtxErrorf(ctx, "GetMenuListByAdminID error: %v", err)
		return nil, err
	}
	return pack.MenuBuild(list), nil
}

func (s *Setting) GetMenuList(ctx context.Context, req *admin_menu.GetMenuListRequest) ([]*admin_menu.AdminMenu, error) {
	var conditions []gen.Condition
	if req.Name != nil {
		conditions = append(conditions, dal.AdminMenu.Name.Eq(req.GetName()))
	}
	list, err := s.dao.AdminMenuDAO.GetAdminMenuList(ctx, conditions, 0, 10000)
	if err != nil {
		hlog.CtxErrorf(ctx, "GetMenuList error: %v", err)
		return nil, err
	}

	return pack.GetMenuList(list), nil
}

func (s *Setting) GetFirstMenuList(ctx context.Context) ([]*admin.Menu, error) {
	list, err := s.dao.AdminMenuDAO.GetAdminMenuList(ctx, []gen.Condition{}, 0, 10000)
	if err != nil {
		hlog.CtxErrorf(ctx, "GetMenuList error: %v", err)
		return nil, err
	}

	return pack.DealMenuList(list, true), nil
}
